package com.corpgovernment.organization.service.impl;

import com.corpgovernment.api.organization.model.backuser.BackgroundUserVo;
import com.corpgovernment.api.organization.model.idcard.UserIdCardVo;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.enums.ExceptionCodeEnum;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.dataloader.db.MbOrgEmployeeLoader;
import com.corpgovernment.organization.entity.db.MbOrgEmployeeInfo;
import com.corpgovernment.organization.entity.db.MbOrgUserIdcard;
import com.corpgovernment.organization.service.IBackgroundUserService;
import com.corpgovernment.organization.service.IOrgUserIdCardService;
import com.corpgovernment.organization.service.IOrganizationEmployeeService;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
 * @author jhxue
 * @create 2022-02-22-15:33
 */
@Service
@Slf4j
public class BackgroundUserService implements IBackgroundUserService {

	@Autowired
	private IOrganizationEmployeeService organizationEmployeeService;
	@Autowired
	private IOrgUserIdCardService organizationUserIdCardService;
	@Autowired
	private MbOrgEmployeeLoader mbOrgEmployeeLoader;

	/**
	 * 通过组织结构id查找父级所有组织结构id，一直到顶级子公司
	 *
	 * @param backgroundUserVo 用户vo
	 * @return JSONResult
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean createOrUpdateUser(@RequestBody BackgroundUserVo backgroundUserVo) {
		if (backgroundUserVo == null || StringUtils.isBlank(backgroundUserVo.getUid())) {
			throw new CorpBusinessException(OrganizationResponseCodeEnum.PARAMS_NULL);

		}
		// 用户基本信息
		MbOrgEmployeeInfo mbOrgEmployeeInfo = new MbOrgEmployeeInfo();
		BeanUtils.copyProperties(backgroundUserVo, mbOrgEmployeeInfo);
		// 通过uid查询一下员工表，判断用户是否存在
		MbOrgEmployeeInfo dbUser = organizationEmployeeService.findAllByUid(backgroundUserVo.getUid());
		// 新增
		if (dbUser == null) {
			MbOrgEmployeeInfo record = new MbOrgEmployeeInfo();
			record.setAreaCode(backgroundUserVo.getAreaCode());
			record.setMobilePhone(backgroundUserVo.getMobilePhone());
			record.setIsBackground(true);
			record.setDeleteTime(new Date(0));
			MbOrgEmployeeInfo oldUser = mbOrgEmployeeLoader.selectOneByRecord(record);
			if (oldUser != null) {
				throw new CorpBusinessException(OrganizationResponseCodeEnum.EMPLOYEE_PHONE_NO_EXIST);
			}
			mbOrgEmployeeInfo.setIsBackground(true);
			boolean flag = organizationEmployeeService.create(mbOrgEmployeeInfo);
			if (!flag) {
				throw new CorpBusinessException(OrganizationResponseCodeEnum.ACTION_FAILED);
			}
			return true;
		} else {
			// 修改
			boolean flag = organizationEmployeeService.update(mbOrgEmployeeInfo);
			if (!flag) {
				throw new CorpBusinessException(OrganizationResponseCodeEnum.ACTION_FAILED);
			}
		}

		// 通过uid和cardType查询是否存在这个证件，没有则新增，否则更新
		if (backgroundUserVo.getCardList() != null) {
			UserIdCardVo userIdCardVo = backgroundUserVo.getCardList().get(0);
			MbOrgUserIdcard typeCard = organizationUserIdCardService.findByEmployeeUidAndType(backgroundUserVo.getUid(), userIdCardVo.getCardType());
			if (typeCard != null && StringUtils.isNotBlank(typeCard.getCardNo())) {
				// 用户证件信息
				MbOrgUserIdcard mbOrgUserIdcard = new MbOrgUserIdcard();
				mbOrgUserIdcard.setCardType(userIdCardVo.getCardType());
				mbOrgUserIdcard.setCardNo(userIdCardVo.getCardNo());
				mbOrgUserIdcard.setEmployeeUid(backgroundUserVo.getUid());
				organizationUserIdCardService.updateEmployeeCard(mbOrgUserIdcard);
			} else {
				MbOrgUserIdcard mbOrgUserIdcard = new MbOrgUserIdcard();
				mbOrgUserIdcard.setCardType(userIdCardVo.getCardType());
				mbOrgUserIdcard.setCardNo(userIdCardVo.getCardNo());
				mbOrgUserIdcard.setEmployeeUid(backgroundUserVo.getUid());
				organizationUserIdCardService.create(mbOrgUserIdcard);
			}
		}
		return true;
	}

	@Override
	public List<MbOrgEmployeeInfo> listAllUsers() {
		return organizationEmployeeService.findAllBackgroundUsers();
	}
}
